<template>
  <div class="login">
    <h1 class="iconfont"></h1>
    <!-- icon-wangyiyunyinyueclick -->
    <div class="form">
      <div class="item">
        <span class="iconfont icon-shouji"></span>
        <input
          type="text"
          placeholder="手机号码"
          v-model="phone"
          autocomplete="off"
        />
      </div>
      <div class="item">
        <span class="iconfont icon-mima"></span>
        <input
          type="password"
          placeholder="密码"
          v-model="password"
          autocomplete="off"
        />
      </div>
      <div class="item">
        <span class="iconfont icon-yanzhengma"></span>
        <input
          type="text"
          placeholder="验证码"
          v-model="code"
          autocomplete="off"
        />
        <button class="getCode" @click="getCode">发送</button>
      </div>
      <div class="item">
        <div class="submit" @click="submit">登录</div>
      </div>
    </div>
  </div>
</template>

<script>
import { getCaptcha, getCellphone } from "@/api/login";
export default {
  data() {
    return {
      code: "",
      password: "",
      phone: "",
    };
  },

  methods: {
    getCode() {
      // 1.验证手机号
      let reg =
        /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
      if (!reg.test(this.phone)) {
        //不正确处理
        this.$toast({ message: "手机号码错误", position: "bottom" });
        return;
      }
      // 2.请求数据接口
      getCaptcha({ phone: this.phone }).then((data) => {
        if (data.data) {
          this.$toast({ message: "验证码发送成功", position: "bottom" });
        } else {
          this.$toast({ message: "请输入正确手机号码", position: "bottom" });
        }
      });
    },

    submit() {
      // 验证验证码
      let reg = /^[0-9]{4}$/;
      if (!reg.test(this.code)) {
        this.$toast({ message: "请输入正确验证码", position: "bottom" });
        return;
      }
      getCellphone({
        phone: this.phone,
        password: this.password,
      }).then((info) => {
        if (info.code == 200) {
          this.$toast({ message: "登录成功", position: "bottom" });
          localStorage.setItem("token", info.token);
          // 跳转个人中心
          sessionStorage.setItem("userid", JSON.stringify(info.account.id));
          this.$router.push({
            path: "/my",
            query: {
              id: info.account.id,
            },
          });
        } else {
          this.$toast({ message: info.message, position: "bottom" });
        }
      });
    },
  },
};
</script>

<style lang="scss" >
.login {
  background-color: #d4473c;
  width: 100%;
  height: 100%;
  color: #fff;
}

h1 {
  font-size: 48px;
  text-align: center;
  height: 48px;
  padding: 150px 0 40px 0;
}

.form {
  height: 667px;

  .item {
    width: 76%;
    height: 30px;
    line-height: 30px;
    margin: 0 auto;
    background-color: #f47378;
    border-radius: 30px;
    margin-bottom: 12px;

    input {
      background-color: transparent;
      border: 0px;
      outline: none;
      text-indent: 0.5em;
      font-size: 14px;
      color: #fff;
    }

    span {
      font-size: 20px;
      margin-left: 10px;
      display: inline-block;
      vertical-align: middle;
    }

    .getCode {
      border: 1px solid #fff;
      height: 20px;
      line-height: 20px;
      width: 50px;
      border-radius: 20px;
      font-size: 12px;
      color: #808080;
      float: right;
      margin: 5px;
    }

    .submit {
      font-size: 15px;
      background-color: #fff;
      text-align: center;
      color: #d4473c;
      border-radius: 15px;
    }
  }
}
</style>